# import numpy as np
import pandas as pd
from sklearn.model_selection import StratifiedKFold
# import lightgbm as lgbm
# import _pickle as pickle
import time
from lightgbm.sklearn import LGBMClassifier
from sklearn.model_selection import GridSearchCV
from scipy.sparse import csr_matrix

import warnings
warnings.filterwarnings("ignore")

begin_time = time.time()


def timer(s=0):
    global begin_time
    if s == 1:
        begin_time = time.time()
    else:
        print(time.time() - begin_time)


X_train = pd.read_pickle("pkl/X_train")
X_train = X_train.sample(n=2000000, random_state=0, axis=0)
print("X_train read")
X_train = csr_matrix(X_train)
print("CSR")
y_train = pd.read_pickle("pkl/y_train")
y_train = y_train.sample(n=2000000, random_state=0, axis=0)

MAX_ROUNDS = 10000
kfold = StratifiedKFold(n_splits=3, shuffle=True, random_state=3)
n_estimators_1 = 863
num_leaves = 63
min_child_samples = 20
subsample = 0.9

params = {'boosting_type': 'gbdt',
          'objective': 'binary',
          'n_jobs': -1,
          'learning_rate': 0.1,
          'n_estimators':n_estimators_1,
          'max_depth': 7,
          'num_leaves': num_leaves,
          'min_child_samples': min_child_samples,
          'max_bin': 127, #2^6,原始特征为整数，很少超过100
          'subsample': subsample,
          'bagging_freq': 1,
          'verbose=': -1
         }
lg = LGBMClassifier(silent=False,  **params)

colsample_bytree_s = [i/10.0 for i in range(3,6)]
tuned_parameters = dict( colsample_bytree = colsample_bytree_s)

grid_search4 = GridSearchCV(lg, n_jobs= -1,  param_grid=tuned_parameters, cv = kfold, scoring="neg_log_loss", verbose=-1, refit = False)
grid_search4.fit(X_train , y_train)

print(-grid_search4.best_score_)
print(grid_search4.best_params_)


fo = open("res.txt", "a+")
fo.write("colsample_bytree: " + str(grid_search4.best_params_) + "\n")
fo.close()
